package cc.mrbird.febs.accounting.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.accounting.entity.Project;
import cc.mrbird.febs.accounting.service.IProjectService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author baoyinlei
 * @date 2021-09-28 23:26:45
 */
@Slf4j
@Validated
@Controller
public class ProjectController extends BaseController {

    @Resource
    private IProjectService projectService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "project")
    public String projectIndex(){
        return FebsUtil.view("project/project");
    }

    @GetMapping("project")
    @ResponseBody
//    @RequiresPermissions("project:list")
    public FebsResponse getAllProjects(Project project) {
        return new FebsResponse().success().data(projectService.findProjects(project));
    }//从数据库中查询数据

    @GetMapping("project/list")
    @ResponseBody
//    @RequiresPermissions("project:list")
    public FebsResponse projectList(QueryRequest request, Project project) {
        Map<String, Object> dataTable = getDataTable(this.projectService.queryProjects(request, project));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping("project/listOngoing")
    @ResponseBody
//    @RequiresPermissions("project:list")
    public FebsResponse ongoingProjectList() {
        List<Project> projects =  projectService.findOngoingProjects();
        return new FebsResponse().success().data(getDataTable(projects, projects.size()));
    }

    @PostMapping("project/updateComment")
    @ResponseBody
    @RequiresPermissions("project:update")
    public FebsResponse updateComment(Project project) {
        this.projectService.updateProjectComment(project.getId(), project.getComment());
        return new FebsResponse().success();
    }

    @PostMapping("project/updateComment2")
    @ResponseBody
    @RequiresPermissions("project:update")
    public FebsResponse updateComment2(Project project) {
        this.projectService.updateProjectComment2(project.getId(), project.getComment2());
        return new FebsResponse().success();
    }

    @GetMapping("project/{companyId}")
    @ResponseBody
//    @RequiresPermissions("project:list")
    public FebsResponse projecByCompanyId(@NotNull(message = "{required}") @PathVariable Long companyId) {
        return new FebsResponse().success().data(this.projectService.getProjectByCompanyId(companyId));
    }

    @ControllerEndpoint(operation = "新增Project", exceptionMessage = "新增Project失败")
    @PostMapping("project")
    @ResponseBody
    @RequiresPermissions("project:add")
    public FebsResponse addProject(@Valid Project project) {
        boolean success =  this.projectService.createProject(project);
        if (success) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("项目名称已存在，请修改!");
        }
    }

    @ControllerEndpoint(operation = "删除Project", exceptionMessage = "删除Project失败")
    @GetMapping("project/delete/{projectIds}")
    @ResponseBody
    @RequiresPermissions("company:delete")
    public FebsResponse deletePorject(@NotBlank(message = "{required}") @PathVariable String projectIds) {
        this.projectService.deleteProject(projectIds);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Project", exceptionMessage = "修改Project失败")
    @PostMapping("project/update")
    @ResponseBody
    @RequiresPermissions("project:update")
    public FebsResponse updateProject(Project project) {
        boolean success = this.projectService.updateProject(project);
        if (success) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("项目名称已存在，请修改!");
        }
    }

    @ControllerEndpoint(operation = "修改Project", exceptionMessage = "导出Excel失败")
    @PostMapping("project/excel")
    @ResponseBody
    @RequiresPermissions("project:export")
    public void export(QueryRequest queryRequest, Project project, HttpServletResponse response) {
        List<Project> projects = this.projectService.queryProjects(queryRequest, project).getRecords();
        ExcelKit.$Export(Project.class, response).downXlsx(projects, false);
    }
}
